﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Modules.DOWNLOAD.Components;

namespace ASPNETVN.PORTAL.Modules.DOWNLOAD.Controls
{
    public partial class ItemManager : AddEditModulePage
    {
        private void buildCategory()
        {
            DataSet ds = Category.SelectAll(this.ModuleID);
            DataRow[] rootRows = ds.Tables[0].Select("ParentID = 0");

            cbCategory.Items.Add(new ListItem("[Tất cả]", "0"));
            foreach (DataRow row in rootRows)
            {
                cbCategory.Items.Add(new ListItem(row["Name"].ToString(), row["ID"].ToString()));
                DataRow[] childRows = ds.Tables[0].Select("ParentID = " + row["ID"]);
                if (childRows.Length > 0)
                {
                    foreach (DataRow childRow in childRows)
                    {
                        cbCategory.Items.Add(new ListItem("» " + childRow["Name"], childRow["ID"].ToString()));
                    }
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                hlAddNew.NavigateUrl = HttpUrlBuilder.BuildUrl(this.ModuleID, 0, ModulePages.AddEditItem);
                this.buildCategory();
            }
        }

        protected void gvArticles_PageIndexChanged(object sender, EventArgs e)
        {
            gvItems.SelectedIndex = -1;
        }

        protected void gvItems_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                HttpUrlBuilder.BuildUrl(this.ModuleID, Convert.ToInt32(e.CommandArgument), ModulePages.AddEditItem);
            }
        }

        protected void hfModuleID_Init(object sender, EventArgs e)
        {
            hfModuleID.Value = this.ModuleID.ToString();
        }
    }
}